<template>
  <div>
    <!-- <TopFilter
      :list="topFilterList"
      :cols="2"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>-->
    <el-checkbox :value="this.autoAllocationflag" @change="handleChange">自动分配</el-checkbox>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog
      :visible.sync="visible"
      :title="dialogTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <setAllocationsModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
    <el-dialog
      :visible.sync="delVisible"
      title="删除"
      :type="handleType"
      :rowData="formData"
      width="30%"
      center
    >
      <span>确定要删除吗?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeVisible">取 消</el-button>
        <el-button type="primary" @click="deleteHandler">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryCluesPrameterCenter, getAutoAllocation, getPartitionById, autoAllocation, delCluesPrameterCenter } from '@/api/potenCus/basedata/cluesPrameter';
import { dictionary } from '@/utils/dictMixin';
import setAllocationsModal from './setAllocationsModal';
export default {
  name: 'setAllocations',
  mixins: [dictionary],
  components: {
    setAllocationsModal
  },
  data() {
    return {
      // topFilterList: this.createTopFilters(),
      fetchParams: {},
      visible: false,
      delVisible: false,
      autoAllocationflag: false,
      dialogTitle: '',
      fetchApi: queryCluesPrameterCenter,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { add: '新增', edit: '编辑', delete: '删除' }
    };
  },
  methods: {
    handleChange(val) {
      this.autoAllocationflag = val;
      const res = autoAllocation({ flag: val });
    },
    createTopFilters() {},
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.delHandle(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '专员名称',
          dataIndex: 'name',
          filter: true,
          filterType: 'input'
        },
        {
          title: '手机号码',
          dataIndex: 'phone',
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否可分配',
          dataIndex: 'isAllot',
          filterType: 'input'
        },
        {
          title: '分配系数',
          dataIndex: 'consultantRatio',
          filterType: 'input'
        },
        {
          title: '服务时间',
          dataIndex: 'workTime',
          width: 200,
          showOverflowTooltip: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      console.log('编辑', row);
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    delHandle(row) {
      console.log('删除', row);
      this.handleType = 'delete';
      this.formData = row;
      this.consultantId = row.consultantId;
      this.dialogTitle = `分配设置${this.conf[this.handleType]}`;
      this.delVisible = true;
    },
    showHandler() {
      this.dialogTitle = `分配设置${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({ sort: 'wayId', order: 'desc' }, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async deleteRow() {
      const res = await delCluesPrameterCenter(this.formData, this.consultantId);
      console.log('删除', res);
      if (res.resultCode === 200) {
        this.closeVisible();
        this.fetchParams = { ...this.fetchParams };
      }
    },
    deleteHandler() {
      this.deleteRow();
    },
    async initAutoAllocation() {
      const res = await getAutoAllocation();
      this.autoAllocationflag = res.data;
    },
    closeVisible() {
      this.delVisible = false;
    }
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.initAutoAllocation();
  },
  created() {}
};
</script>

<style lang="less" scoped>
</style>
